package com.hsav.kcxb.adapter;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.hsav.kcxb.R;
import com.hsav.kcxb.bean.UsertItemBean;

import java.util.ArrayList;

/**
 * Created by zhao on 2017/1/8.
 */

public class UserItemAdapter extends RecyclerView.Adapter<UserItemAdapter.MyViewHolder>{

    private final Context mContext;
    private final LayoutInflater mInflater;
    private final ArrayList<UsertItemBean> mUserItemList;
    private ArrayList<UsertItemBean> userItemList = new ArrayList<>();
    private OnHanldeClickListener mHandleClickListener;
    public static final int TYPE_MY_CENTER = 0;
    public static final int TYPE_OTHER_CENTER = 1;

    public UserItemAdapter(Context context,ArrayList<UsertItemBean> itemList){
        this.mContext =context;
        this.mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mUserItemList = itemList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_user_layout,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if(mUserItemList!=null && mUserItemList.size()>position){
            holder.setData(mUserItemList.get(position));
        }
    }


    @Override
    public int getItemCount() {
        if(mUserItemList!=null){
            return mUserItemList.size();
        }
        return 8;
    }

    public interface OnHanldeClickListener{
        void handleClick(UsertItemBean itemBean,int type);
    }

    public void setOnHandleClickListener(OnHanldeClickListener listener){
        this.mHandleClickListener = listener;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        public MyViewHolder(View itemView) {
            super(itemView);
            RelativeLayout layoutTop = (RelativeLayout)itemView.findViewById(R.id.rl_item_top);
            RelativeLayout layoutBottom = (RelativeLayout)itemView.findViewById(R.id.rl_item_bottom);

            layoutTop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mHandleClickListener!=null){
                        mHandleClickListener.handleClick(null,TYPE_MY_CENTER);
                    }
                }
            });

            layoutBottom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mHandleClickListener!=null){
                        mHandleClickListener.handleClick(null,TYPE_OTHER_CENTER);
                    }
                }
            });
        }

        public void setData(UsertItemBean itemBean){

        }
    }

    public static class SpaceItem extends RecyclerView.ItemDecoration{
        private final int mSpace;

        public SpaceItem(int space){
            this.mSpace = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int pos = parent.getChildAdapterPosition(view);
            int count = parent.getChildCount();
            if(pos!=count-1){
                outRect.bottom = mSpace;
            }
        }
    }
}
